﻿/****************************** Module Header ******************************\
 * Module Name:  TaskHelper.cs
 * Module Version:  1.0.0 (2014-4-7)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This class is used to send email by Net MailMessage dll. It supplies 
 * useful method to send a email by SMTP server.
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace UCF.Core
{
    /// <summary>
    /// 异步任务帮助类
    /// </summary>
    /// <remarks>2014-4-7 by sutroon</remarks>
    public class TaskHelper<T> : TaskHelper
    {
        public Task<T> Task;
        public void NewTask(Func<T> func)
        {
            //创建任务，并采用默认任务调度器（线程池任务调度器）执行任务
            Task = new Task<T>(() => { return func(); });
        }
        public void ContinueWith(Action act)
        {
            TaskScheduler m_syncContextTaskScheduler = TaskScheduler.FromCurrentSynchronizationContext();
            var cts = new CancellationTokenSource();
            //任务完成时启动一个后续任务，并采用同步上下文任务调度器调度任务更新UI组件。
            Task.ContinueWith(t => { HideLoading(); act(); }, cts.Token, TaskContinuationOptions.AttachedToParent, m_syncContextTaskScheduler);
        }
        public void Start()
        {
            Task.Start();
        }
    }
}
